package me.pearjelly.pmc.search;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;

public class CheckIndexes {
	private static String indexDir = "/opt/pms/index/data";
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			IndexSearcher searcher = new IndexSearcher(FSDirectory
					.open(new File(indexDir)));
			List<Document> list = getBy(searcher, "id", "242673");
			for (Document document : list) {
				System.out.println(document);
			}
			searcher.close();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}
	
	public static List<Document> getBy(IndexSearcher searcher, String proName, String value){
		Query query = new TermQuery(new Term(proName, value));
		List<Document> list = new LinkedList<Document>();
		try {
			TopDocs topDocs = searcher.search(query, 100);
			ScoreDoc[] scoreDocs = topDocs.scoreDocs;
			for (ScoreDoc scoreDoc : scoreDocs) {
				list.add(searcher.doc(scoreDoc.doc));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
}
